import DataLoader from 'dataloader'
import { connection } from './conn_to_sqlite.js'

// hard code is not a good way ? TODO: move to .env file
const getCompantTable = () => connection.table('company');

// get a company by id
export async function getCompany(id) {
	return await getCompantTable().first().where({ id })
}

// load all company by id ?
export function createCompanyLoader() {
	return new DataLoader(async (ids) => {
		const companies = await getCompantTable().select()
			.whereIn('id', ids);
		return ids.map((id) => companies.find((company) => company.id === id))
	})
}
